<?php

namespace app\Modules\Api\controllers;



use Yii;
use app\Modules\Api\models\Auction;
use app\Modules\Api\models\Product;
use app\Modules\Api\models\Type;
use app\Modules\Api\models\User;
use app\filters\Json;
use yii\data\Pagination;
use app\helpers\Request;
use yii\db\ActiveQuery;



/**
 * Class ProductController
 * @package app\Modules\Api\controllers
 */
class ProductController extends ControllerBase
{
    /**
     * 商品列表
     * @return array
     */
    public function actionIndex()
    {
        $first_auctions = Auction::getFirsts();
        $type_objects = Type::find()->all();
        $product_objects = Product::find()->orderBy(['pid'=>SORT_DESC])->all();
        $fields = ['pid','name','type_id','spec'];
        $extraFields = ['first_pic'];
        $old_types = [];
        foreach($product_objects as $product_object){
            if(isset($first_auctions[$product_object->pid])){
                $product = $product_object->toArray($fields,$extraFields);
                $product['first_auction'] = $first_auctions[$product_object->pid]->toArray(['actcount','reacount','price','state']);
                $old_types[$product_object->type_id]['products'][] = $product;
            }
        }
        $types = [];
        foreach($type_objects as $type_object){
            if(isset($old_types[$type_object->typeid])){
                $type = $type_object->toArray(['name']);
                $type['products'] = $old_types[$type_object->typeid]['products'];
                $types[] = $type;
            }
        }
        $data = $types;
        return Json::success($data);
    }
    /**
     * 十元专区
     * @return array
     */
    public function actionTen()
    {
        $query = Product::find()->where(['type_id'=>Type::TEN]);
        $count = $query->count();

        $pagination = new Pagination([
            'totalCount' => $count,
            'pageSize'=>Yii::$app->request->post('pageSize',6),
            'page'=>Yii::$app->request->post('page'),
        ]);

        $products = $this->getProducts($query,$pagination);

        $data = [
            'products'=>$products,
            'product_count'=>$count,
        ];
        return Json::success($data);
    }

    /**
     * 百元专区
     * @return array
     */
    public function actionHundred()
    {
        $query = Product::find()->where(['type_id'=>Type::HUNDRED]);
        $count = $query->count();

        $pagination = new Pagination([
            'totalCount' => $count,
            'pageSize'=>Yii::$app->request->post('pageSize',6),
            'page'=>Yii::$app->request->post('page'),
        ]);

        $products = $this->getProducts($query,$pagination);

        $data = [
            'products'=>$products,
            'product_count'=>$count,
        ];
        return Json::success($data);
    }

    /**
     * 收藏/取消收藏
     * @return array
     */
    public function actionCollectornot()
    {
        $user_id = Request::strictpost('user_id','缺少用户id');
        $product_id = Request::strictpost('product_id','缺少产品id');

        $product = Product::findOne($product_id);
        $user = User::findOne($user_id);
        if($user&&$product){
            $isLink = $product->collectOrNot($user);
            return Json::success([],$isLink?'收藏成功':'取消成功');
        }
        return Json::error('参数错误');
    }

    /**
     * 我的收藏
     * @return array
     */
    public function actionCollection()
    {
        $user_id = Request::strictpost('user_id','缺少用户id');
        /** @var  $user User */
        $user = User::findOne($user_id);
        if(!$user)
            return Json::error('用户不存在');
        /** @var  $query yii\db\ActiveQuery*/
        $query = $user->getCollects();
        $count = $query->count();
        $pagination = new Pagination([
            'totalCount' => $count,
            'pageSize'=>Yii::$app->request->post('pageSize',6),
            'page'=>Yii::$app->request->post('page'),
        ]);

        $products = $this->getProducts($query,$pagination);

        $data = [
            'products'=>$products,
            'product_count'=>$count,
        ];

        return Json::success($data);
    }

    /**
     * @param $query ActiveQuery
     * @param $pagination Pagination
     * @return array
     */
    protected function getProducts($query,$pagination){
        /** @var $productObjects Product[] */
        $productObjects = $query
            ->orderBy(['pid'=>SORT_DESC])
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();
        $fields = ['pid','name','type_id','spec'];
        $extraFields = ['first_pic'];
        $products = [];
        $first_auctions = Auction::getFirsts();
        foreach($productObjects as $key =>$object){
            if(isset($first_auctions[$object->pid])){
                $product = $object->toArray($fields,$extraFields);
                $product['first_auction'] = $first_auctions[$object->pid]->toArray(['actcount','reacount','price','state']);
                $products[] = $product;
            }
        }
        return $products;
    }
}